//
//  RootViewController.m
//  PostDemo1
//
//  Created by  江志磊 on 15-1-29.
//  Copyright (c) 2015年  江 志磊. All rights reserved.
//

#import "RootViewController.h"
#import "MyPostRequest.h"
#import "RegularHelp.h"

@interface RootViewController ()
{
    MyPostRequest *_postRequest;
}
@end

@implementation RootViewController

#define kRegister @"http://open.doushangshang.com/user.php?ac=register"
//登录
#define kLogin @"http://open.doushangshang.com/user.php?ac=login"

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _postRequest = [[MyPostRequest alloc] init];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//注册
- (IBAction)registerBtnClicked:(id)sender {
    if (_userNameField.text.length==0||_passwordField.text.length==0) {
        NSLog(@"信息输入不全!");
        return;
    }
    //判断用户名是否为邮箱
    if ([RegularHelp validateUserEmail:_userNameField.text]==NO) {
        NSLog(@"邮箱格式不正确!");
        return;
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_userNameField.text,@"user",_passwordField.text,@"password",_passwordField.text,@"conform_password",nil];
    [_postRequest  requestWithUrlString:kRegister parms:dic contentType:@"application/x-www-form-urlencoded" requestMethod:@"POST" finishedBlock:^(NSData *downloadData) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"register dic:%@",dic);
        
    } failedBlock:^(NSString *erroMsg) {
        
    }];
}

//登录
- (IBAction)loginBtnClicked:(id)sender {
    if (_userNameField.text.length==0||_passwordField.text.length==0) {
        NSLog(@"信息输入不全!");
        return;
    }
    //判断用户名是否为邮箱
    if ([RegularHelp validateUserEmail:_userNameField.text]==NO) {
        NSLog(@"邮箱格式不正确!");
        return;
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_userNameField.text,@"user",_passwordField.text,@"password",nil];
    [_postRequest requestWithUrlString:kLogin parms:dic contentType:@"application/x-www-form-urlencoded" requestMethod:@"POST" finishedBlock:^(NSData *downloadData) {
        NSDictionary *msgDic = [NSJSONSerialization JSONObjectWithData:downloadData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"login dic:%@",msgDic);
        //sessionid:服务器根据用户账号和密码信息进行加密处理，生成的用户账号的密钥,密钥代表用户身份信息,密钥一段时间后会失效,用户需从新登录获取新的密钥
        
        
    } failedBlock:^(NSString *erroMsg) {
        NSLog(@"login error:%@",erroMsg);
    }];
    
    
}
@end
